﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CoreSolution.Dapper.Api.Model;
using Entity;
using IServices;
using Microsoft.AspNetCore.Mvc;
using Services;

namespace CoreSolution.Dapper.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        // GET api/values
        [HttpGet]
        public ActionResult<IEnumerable<string>> Get()
        {

            IUserServices _IUserServices = new UserServices();

            var Result1=_IUserServices.Where(t => t.UserName == "lwj").OrderBy(t => t.UserName).Skip(0).Take(3).ToList();

            #region 查询
            //_IUserServices.where(t=>t.username=="lwj").orderBy(t=>t.createDateTime).skip(3).take(3).toList();
            var Result2= _IUserServices.Where(t => t.UserName == "lwj").OrderBy(t => t.UserName).Skip(0).Take(3).ToList();
            

            //_IUserServices.firstDefault(t => t.username == "lwj");
            var Result3=_IUserServices.FirstOrDefault(t=>t.UserName=="lwj");

            //_IUserServices.where(t=>t.username=="lwj").tolist();
            var Result4=_IUserServices.Where(t => t.UserName == "lwj").ToList();

            
            #endregion


            #region 查询Selector 推荐匿名方式

            //ToList Selector T
            var Result5 = _IUserServices.Where(t => t.UserName == "lwj").OrderBy(t => t.UserName).Skip(0).Take(3).Select(t => new UserInfo()
            {
                Id = t.Id,
                UserName = t.UserName
            }).ToList();

           
            //ToList Selector T1
            var Result6 = _IUserServices.Where(t => t.UserName == "lwj").OrderBy(t => t.UserName).Skip(0).Take(3).Select(t => new _VUserInfo()
            {
                Id = t.Id,
                UserName = t.UserName
            }).ToList<_VUserInfo>();


            //ToList Selector 匿名(推荐，清爽)
            var Result7 = _IUserServices.Where(t => t.UserName == "lwj").OrderBy(t => t.UserName).Skip(0).Take(3).Select(t => new
            {
                Id = t.Id,
                UserName = t.UserName
            }).ToList<_VUserInfo>();

            #endregion


           

            #region 添加
            //_IUserServices.add(model);
            var addNum = _IUserServices.Add(new UserInfo() {Id=Guid.NewGuid(), UserAge=22, UserName="lewis23", UserSex=true });
            //_IUserServices.saveChange();
            _IUserServices.SaveChange(addNum);
            #endregion

            #region 更新 推荐Selector

            //_IUserServices.Update(model); 全量更新操作
            var idd = Guid.Parse("F62740B1-9187-43D1-A89D-40A72EE9F635");
            var Update_iddModel=_IUserServices.FirstOrDefault(t => t.Id == idd);
            Update_iddModel.UserName = "23货";
            var updateNum = _IUserServices.Update(Update_iddModel);
            _IUserServices.SaveChange(updateNum);

            //_IUserServices.Update(t=>model); 更新操作 Selector 推荐
            var id_s = Guid.Parse("3F3AA815-ADB0-4513-96CA-416837BC7DC1");
            var updateSelectorNum = _IUserServices.Update(t => new UserInfo()
            {
                Id = id_s,
                UserName = "2或欧巴撒",
                UserAge=30,
            });
            _IUserServices.SaveChange(updateSelectorNum);


            #endregion

            #region 删除

            //_IUserServices.Delete(id)
            var deleteNum =_IUserServices.Delete(Guid.Parse("FBE55EE3-8AD5-46BC-BEBD-F04A8A37BC09"));
            _IUserServices.SaveChange(deleteNum);

            //_IUserServices.Delete(id)
            var id = Guid.Parse("9E88E4C1-E47F-44FE-B028-27AB646CF6F3");
            var firstModelDefault=_IUserServices.FirstOrDefault(t => t.Id == id);
            var deleteModelNum = _IUserServices.Delete(firstModelDefault);
            _IUserServices.SaveChange(deleteModelNum);

            //_IUserServices.Remove(id)
            var removeNum=_IUserServices.Remove(Guid.Parse("23FD0BB1-44BF-455B-9C3F-32F10DE90BE6"));
            _IUserServices.SaveChange(removeNum);
            #endregion

            return new string[] { "value1", "value2" };
        }
    }
}
